//
//  ZCEnrollButtonView.m
//  zctx
//
//  Created by apple on 16/2/22.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCEnrollButtonView.h"
#import "MeButton.h"
@interface ZCEnrollButtonView()

@property (nonatomic,strong)UIImageView *bgImgView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation ZCEnrollButtonView

-(id)initWithFrame:(CGRect)frame andType:(NSString *)str{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        if ([str isEqualToString:@"0"]) {
            [self steUp:nil];
        }else{
            [self steUp:nil];
            [self addLables];
        }
    }
    return self;
}

-(void)setMModel:(ZCRankingListModelMoreData *)mModel{
    _mModel = mModel;
    for (int  i = 0; i<3; i++) {
        MeButton *sendButton = (MeButton *)self.dataArray[i];
        if (i==0) {
            [sendButton.contentLable setText:[NSString stringWithFormat:@"%.0f",mModel.voteCount]];
        }else if (i==1){
            [sendButton.contentLable setText:[NSString stringWithFormat:@"%.0f",mModel.joinCount]];
        }else{
            [sendButton.contentLable setText:[NSString stringWithFormat:@"%.0f",mModel.viewCount]];
        }
    }
}

-(void)steUp:(NSDictionary *)dict{
    NSArray *titleArray = [[NSArray alloc]init];
    if (dict) {
        NSString *takepart_count = [NSString stringWithFormat:@"%@",[dict objectForKey:@"redpack_count"]];
        NSString *follow_founding_count = [NSString stringWithFormat:@"%@",[dict objectForKey:@"my_takepart_count"]];
        NSString *beans = [NSString stringWithFormat:@"%@",[dict objectForKey:@"beans"]];
        BOOL isnull = [NSString isNull:takepart_count];
        
        if (isnull == YES) {
            titleArray = @[@"0",@"0",@"0"];
        }else{
            titleArray = @[takepart_count,follow_founding_count,beans];
        }
    }else{
        titleArray = @[@"0",@"0",@"0"];
    }
    if (UserModel.password.length==1) {
        titleArray = @[@"0",@"0",@"0"];
    }
    NSLog(@"titleArray = %@",titleArray);
    NSArray *contentArray = [NSArray arrayWithObjects:@"参与人",@"投票数",@"浏览量",nil];
    
    CGFloat buttonW = (SCREEN_WIDTH-40)/3;
    CGFloat marge = 10;
    for (int i = 0; i <titleArray.count; i++) {
        CGRect rect = CGRectMake(i * (buttonW+marge)+marge,32, buttonW, 45);
        MeButton *btnView = [[MeButton alloc] initWithFrame:rect];
        btnView.titleLabel.font = [UIFont systemFontOfSize:13];
        btnView.title = contentArray[i];
        btnView.content = titleArray[i];
        btnView.tag = i;
        btnView.layer.cornerRadius = 5;
        [self addSubview:btnView];
        btnView.titleLabel.textColor = [UIColor whiteColor];
        btnView.contentLable.textColor = [UIColor whiteColor];
        [self.dataArray addObject:btnView];
        [btnView addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i==0) {
            btnView.backgroundColor = [UIColor colorWithHexString:@"#4CE1C3"];
        }else if (i==1){
            btnView.backgroundColor = [UIColor colorWithHexString:@"#FF966C"];
        }else{
            btnView.backgroundColor = [UIColor colorWithHexString:@"#FFBE3C"];
        }
    }
}

-(void)addLables{
    UILabel *Elable = [[UILabel alloc]initWithFrame:CGRectZero];
    [self addSubview:Elable];
    Elable.text = @"报名处";
    Elable.textAlignment = NSTextAlignmentCenter;
    Elable.font = [UIFont boldSystemFontOfSize:25];
    Elable.textColor = [UIColor colorWithHexString:@"#2DBEFF"];
    [Elable makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(108);
        make.centerX.equalTo(self.centerX).with.offset(0);
        make.width.mas_equalTo(@100);
        make.height.mas_equalTo(@25);
    }];
    
    CGFloat bgimg = SCREEN_WIDTH/6.46;
    [self addSubview:self.bgImgView];
    [self.bgImgView makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(Elable.bottom).with.offset(0);
        make.bottom.equalTo(self.bottom).with.offset(0);
        make.left.equalTo(self).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.height.mas_equalTo(bgimg);
    }];
}
-(void)buttonClick:(UIButton *)sender{
    NSInteger index = sender.tag;
    self.Ebutton(index);
//    if ([_delegate respondsToSelector:@selector(didSelectMeSecondCellIndex:)]) {
//        [_delegate didSelectMeSecondCellIndex:index];
//    }
//}
}


-(UIImageView *)bgImgView{
    if (!_bgImgView) {
        _bgImgView = [[UIImageView alloc]init];
        _bgImgView.image = [UIImage imageNamed:@"enroll_lang"];
        _bgImgView.backgroundColor = RGB(241, 251, 253);
//        _bgImgView.backgroundColor = [UIColor orangeColor];
    }
    return _bgImgView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}

@end
